Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): new worker pkg #3979

Merged
merged 1 commit into from
Jul 25, 2024
Merged

feat(api): new worker pkg #3979

merged 1 commit into from
Jul 25, 2024

Conversation

heiytor
Copy link
Contributor

@heiytor heiytor commented Jul 22, 2024

We're refactoring the old api/workers into a proper package under pkg. The new package includes two interfaces: Server, which registers for process events, and Client, which submits events.

A server can listen for events by calling HandleTask. Cron jobs are also supported via HandleCron. Several structs and methods have been added to facilitate task and cron processing.

The new worker/asynq package has been created to implement these interfaces. The package uses Asynq as the backend for the worker. Different implementations can be created to implement the interfaces.

Example usage:

func main() {
    server := asynq.NewServer("...")
    defer server.Shutdown()

    server.HandleTask(
        "queue:kind",
        func(_ context.Context, payload []byte) error {
            fmt.Println("Executing task with payload "+string(payload))
            return nil
        },
    )
    
    if err := server.Start(); err != nil {
        panic(err)
    }

    client, err := asynq.NewClient("...")
    if err != nil {
        panic(err)
    }
    defer client.Close()

    _ = client.Submit(context.Background(), "queue:kind", []byte("payload"))

    os.Exit(0)
}

@heiytor heiytor self-assigned this Jul 22, 2024
@heiytor heiytor force-pushed the feat/workers branch 4 times, most recently from 797f796 to 118ec01 Compare July 23, 2024 16:19
@heiytor heiytor marked this pull request as ready for review July 23, 2024 16:19
@heiytor heiytor requested review from a team as code owners July 23, 2024 16:19
@heiytor heiytor force-pushed the feat/workers branch 5 times, most recently from ad9f36b to a3742cc Compare July 24, 2024 20:43
We're refactoring the old `api/workers` into a proper package under
`pkg`. The new package includes two interfaces: `Server`, which
registers for process events, and `Client`, which submits events.

A server can listen for events by calling `HandleTask`. Cron jobs are
also supported via `HandleCron`. Several structs and methods have been
added to facilitate task and cron processing.

The new `worker/asynq` package has been created to implement these
interfaces. The package uses Asynq as the backend for the worker.
Different implementations can be created to implement the interfaces.

Example usage:
```go
func main() {
    server := asynq.NewServer("...")
    defer server.Shutdown()

    server.HandleTask(
        "queue:kind",
        func(_ context.Context, payload []byte) error {
            fmt.Println("Executing task with payload "+string(payload))
            return nil
        },
    )

    if err := server.Start(); err != nil {
        panic(err)
    }

    client, err := asynq.NewClient("...")
    if err != nil {
        panic(err)
    }
    defer client.Close()

    if err := client.Submit(
        context.Background(),
        "queue:kind",
        []byte("payload"),
    ); err != nil {
        panic(err)
    }

    os.Exit(0)
}
```
@gustavosbarreto gustavosbarreto merged commit 82e415b into master Jul 25, 2024
17 checks passed
@gustavosbarreto gustavosbarreto deleted the feat/workers branch July 25, 2024 12:31
heiytor added a commit that referenced this pull request Jul 25, 2024
The old `api/workers` has been removed in favor of the new worker
package (#3979). The workers' initialization has also been moved to the
`startServer` function to ensure proper shutdown within the router.
heiytor added a commit that referenced this pull request Jul 25, 2024
To support the new worker package, we are replacing the direct usage of
`asynq.Client` with the new `worker.Client` (#3979). The client
initialization process has also been updated to return an error if the
worker cannot be created.
heiytor added a commit that referenced this pull request Jul 25, 2024
The old `api/workers` has been removed in favor of the new worker
package (#3979). The workers' initialization has also been moved to the
`startServer` function to ensure proper shutdown within the router.
heiytor added a commit that referenced this pull request Jul 25, 2024
To support the new worker package, we are replacing the direct usage of
`asynq.Client` with the new `worker.Client` (#3979). The client
initialization process has also been updated to return an error if the
worker cannot be created.
gustavosbarreto pushed a commit that referenced this pull request Jul 26, 2024
The old `api/workers` has been removed in favor of the new worker
package (#3979). The workers' initialization has also been moved to the
`startServer` function to ensure proper shutdown within the router.
gustavosbarreto pushed a commit that referenced this pull request Jul 26, 2024
To support the new worker package, we are replacing the direct usage of
`asynq.Client` with the new `worker.Client` (#3979). The client
initialization process has also been updated to return an error if the
worker cannot be created.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants